package buildcode.support;

import java.util.HashMap;
import java.util.Map;

import buildcode.Context;
import buildcode.build.BuildCommand;
import buildcode.build.BuildCommandBuilder;
import buildcode.build.ICommandExecute;
import buildcode.language.es.EsUtil;
import buildcode.language.es.command.EsCommandExecute;
import buildcode.language.es.command.EsEnumCommandExecute;
import buildcode.language.java.command.JavaEntityCommandExecute;
import buildcode.language.java.command.JavaEnumCommandExecute;
import buildcode.language.java.command.JavaXmlMapperCommandExecute;
import buildcode.language.simple.DefaultCommandExecute;

public class SmartBuildCommndBuilder extends BuildCommandBuilder {
    private String tplDir;
    private static Map<String, DefaultParam> paramMap = new HashMap<>();

    static {
        paramMap.put(ContextIdConstant.CONTEXT_ID_CONTROLLER, new DefaultParam("Controller.vm", new JavaEntityCommandExecute(), "Controller"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_SERVICE, new DefaultParam("Service.vm", new JavaEntityCommandExecute(), "Service"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_SERVICE_IMPL, new DefaultParam("ServiceImpl.vm", new JavaEntityCommandExecute(), "ServiceImpl"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_VO, new DefaultParam("Vo.vm", new JavaEntityCommandExecute(), "Vo"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_DTO, new DefaultParam("Dto.vm", new JavaEntityCommandExecute(), "DTO"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_ENTITY, new DefaultParam("Entity.vm", new JavaEntityCommandExecute(), "Entity"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_QUERY, new DefaultParam("Query.vm", new JavaEntityCommandExecute(), "Query"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_JAVA_MAPPER, new DefaultParam("Mapper.vm", new JavaEntityCommandExecute(), "Mapper"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_JAVA_MAPPER_IMPL, new DefaultParam("MapperImpl.vm", new JavaEntityCommandExecute(), "MapperImpl"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_CONVERTER, new DefaultParam("Converter.vm", new JavaEntityCommandExecute(), "Converter"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_LISTREQUEST, new DefaultParam("ListRequest.vm", new JavaEntityCommandExecute(), "ListRequest"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_PAGEREQUEST, new DefaultParam("PageRequest.vm", new JavaEntityCommandExecute(), "PageRequest"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_XML_MAPPER, new DefaultParam("XmlMapper.vm", new JavaXmlMapperCommandExecute(), "Mapper"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_ENUM, new DefaultParam("Enum.vm", new JavaEnumCommandExecute(), "Enum"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_FEIGN_API, new DefaultParam("FeignApi.vm", new JavaEntityCommandExecute(), "Api"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_FEIGN_CONTROLLER, new DefaultParam("FeignController.vm", new JavaEntityCommandExecute(), "Controller"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_ADDREQUEST, new DefaultParam("AddRequest.vm", new JavaEntityCommandExecute(), "AddRequest"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_UPDATEREQUEST, new DefaultParam("UpdateRequest.vm", new JavaEntityCommandExecute(), "UpdateRequest"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_DELREQUEST, new DefaultParam("DelRequest.vm", new JavaEntityCommandExecute(), "DelRequest"));
        //前端
        paramMap.put(ContextIdConstant.CONTEXT_ID_FORNT_API_PAGE, new DefaultParam("ApiPage.vm", new EsCommandExecute(), "Api"));
        paramMap.put(ContextIdConstant.CONTEXT_ID_FORNT_TYPE_PAGE, new DefaultParam("TypePage.vm", new EsCommandExecute(), ""));
        paramMap.put(ContextIdConstant.CONTEXT_ID_FORNT_ENUM_PAGE, new DefaultParam("EnumPage.vm", new EsEnumCommandExecute(), "Enums"));

        // 其他
        paramMap.put(ContextIdConstant.CONTEXT_ID_DOC, new DefaultParam("Doc.vm", new DefaultCommandExecute(), ""));
        Context.addVelocityTool("SmartContext", SmartContext.class);
        Context.addVelocityTool("EsUtil", EsUtil.class);
    }

    private SmartBuildCommndBuilder(String contextId, String tplDir) {
        super();
        setContextId(contextId);
        this.tplDir = tplDir;
    }

    public static SmartBuildCommndBuilder of(String contextId, String tplDir) {
        return new SmartBuildCommndBuilder(contextId, tplDir);
    }

    @Override
    public BuildCommand build() {
        DefaultParam param = paramMap.get(getContextId());
        if (getTpl() == null) {
            setTpl(tplDir + "/" + param.tplFileName);
        }
        if (getExecute() == null) {
            setExecute(param.command);
        }
        if (getSuffix() == null) {
            setSuffix(param.suffix);
        }
        return super.build();
    }

    record DefaultParam(String tplFileName, ICommandExecute command, String suffix) {
    }
}
